//
//  CBJComposeController.swift
//  Weibo
//
//  Created by 崔健 on 16/5/28.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit
import SVProgressHUD


class CBJComposeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    func setNavUI(){
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Done , target: self, action: #selector(closeVc))
        let navView = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 40))
        let titleLabel : UILabel = UILabel(text: "发布微博", font: 18,textColor: UIColor.darkGrayColor())
        let nameLabel : UILabel = UILabel(text: CBJUserAccountViewModel.sharedAccountViewModel.userAccount!.name!, font: 12 , textColor: UIColor.darkGrayColor())
        navView.addSubview(titleLabel)
        navView.addSubview(nameLabel)
        titleLabel.snp_makeConstraints { (make) in
            
            make.top.left.right.equalTo(navView)
            make.centerX.equalTo(navView)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(navView)
            make.top.equalTo(titleLabel.snp_bottom)
        }
        
        navigationItem.titleView = navView
        rightBarbuttonItem.enabled = false
        navigationItem.rightBarButtonItem = rightBarbuttonItem
    }
    
    private lazy var textView : UITextView = UITextView()
    private lazy var placeHolderLabel : UILabel = {
    
        let lable = UILabel(text: "德玛西亚", font: 12)
        return lable
        
    }()
    
    func setTextView(){
    
        view.addSubview(textView)
        
        textView.snp_makeConstraints { (make) in
            
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(300)
        }
        
        textView.addSubview(placeHolderLabel)
        
        placeHolderLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(textView).offset(5)
            make.top.equalTo(textView).offset(8)
        }
        
        
        
        textView.delegate = self
    }
    
    private var toolBar : UIToolbar = UIToolbar()
    func setToolUI(){
    
        view.addSubview(toolBar)
        
        toolBar.snp_makeConstraints { (make) in
            
            make.left.bottom.right.equalTo(view)
        }
        
        let itemSettings = [["imageName": "compose_toolbar_picture","actionName": "selectPicture"],
                            ["imageName": "compose_mentionbutton_background","actionName": "selectPicture"],
                            ["imageName": "compose_trendbutton_background","actionName": "selectPicture"],
                            ["imageName": "compose_emoticonbutton_background", "actionName": "selectEmoticon"],
                            ["imageName": "compose_add_background","actionName": "selectPicture"]]
        
        var items : [UIBarButtonItem] = [UIBarButtonItem]()
        for item in itemSettings{
        
            let imageName : String = item["imageName"]!
            let action : String = item["actionName"]!
            let barBtn = UIBarButtonItem()
            
            barBtn.action = Selector(action)
            barBtn.image = UIImage.init(named: imageName)
            items.append(barBtn)
            items.append(UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil))
        }
        
        items.removeLast()
        toolBar.items = items
        
    }

    func setupUI(){
        
        setNavUI()
        setTextView()
        setToolUI()

    }

    
    func closeVc(){
    
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    private lazy var rightBarbuttonItem: UIBarButtonItem = {
        let btn = UIButton()
        //设置文案
        btn.setTitle("发送", forState: .Normal)
        //添加点击事件
        btn.addTarget(self, action:#selector(send) , forControlEvents: .TouchUpInside)
        //设置根据内容显示
        //设置文字颜色
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Disabled)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_orange"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Disabled)
        btn.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.frame = CGRect(x: 0, y: 0, width: 45, height: 30)
        let barButtonItem = UIBarButtonItem(customView: btn)
        
        return barButtonItem
    }()
    
    
    func send(){
    
        CBJNetworkTools.sharedInstance.sendStatues(textView.text) { (responseObject, error) in
            
            if error != nil{
            
                SVProgressHUD.showErrorWithStatus("发布失败")
                return
            }
            
            SVProgressHUD.showSuccessWithStatus("发布成功")
        }
    }
    
}


extension CBJComposeController : UITextViewDelegate{

    func textViewDidChange(textView: UITextView) {
        
        placeHolderLabel.text = textView.hasText() ? "" : "德玛西亚"
        
        rightBarbuttonItem.enabled = textView.hasText()
    }
    
}


